package com.luoluo.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import org.codehaus.groovy.tools.shell.IO;

import com.luoluo.bean.Document;
import com.luoluo.dao.DocumentDao;
import com.luoluo.service.DocumentService;
import com.luoluo.utils.CommonParser;
import com.luoluo.utils.ExcelParser;
import com.luoluo.utils.PDFParser;
import com.luoluo.utils.PPTParser;
import com.luoluo.utils.Parser;
import com.luoluo.utils.WordParser;
import com.luoluo.view.ViewTools;

public class DocumentAction extends BaseAction {
	private static final long serialVersionUID = 1369236791239786599L;
	private static final String WWW = "/WEB-INF/www";
	private static final String VM = ".vm";
	private static final String INDEX = WWW + "/" + "index" + VM;
	private DocumentService service;
	private String title;
	private byte type;
	private String author;
	private String isbn;
	private String content;
	private String path;
	private String publish;
	private String publish_time;
	private File docFile;
	private String docFileContentType;
	private String docFileFileName;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public byte getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getPublish() {
		return publish;
	}

	public void setPublish(String publish) {
		this.publish = publish;
	}

	public String getPublish_time() {
		return publish_time;
	}

	public void setPublish_time(String publish_time) {
		this.publish_time = publish_time;
	}

	public DocumentService getService() {
		return service;
	}

	public void setService(DocumentService service) {
		this.service = service;
	}

	public File getDocFile() {
		return docFile;
	}

	public void setDocFile(File docFile) {
		this.docFile = docFile;
	}

	public String getDocFileContentType() {
		return docFileContentType;
	}

	public void setDocFileContentType(String docFileContentType) {
		this.docFileContentType = docFileContentType;
	}

	public String getDocFileFileName() {
		return docFileFileName;
	}

	public void setDocFileFileName(String docFileFileName) {
		this.docFileFileName = docFileFileName;
	}

	public static final Map<String, Parser> PARSERS = new HashMap<String, Parser>() {
		{
			put("dps", new PPTParser());
			put("ppt", new PPTParser());
			put("doc", new WordParser());
			put("rtf", new WordParser());
			put("wps", new WordParser());
			put("pdf", new PDFParser());
			put("xls", new ExcelParser());
			put("xxls", new ExcelParser());
			put("et", new ExcelParser());
		}
	};

	public String insert() throws Exception {
		Document doc = new Document();
		try {
			ServletContext context = ServletActionContext.getServletContext();
			String path = context.getRealPath("/uploads");
			File destFile = new File(path, docFileFileName);
			if (!destFile.getParentFile().exists()) {
				destFile.getParentFile().mkdirs();
			}
			FileUtils.copyFile(docFile, destFile);
			doc.setTitle(this.title);
			doc.setAuthor(this.author);
			if (null == this.content && null != destFile) {
				String ext = FilenameUtils.getExtension(destFile.getName());
				Parser parser = PARSERS.get(ext);
				if (null == parser) {
					parser = new CommonParser();
				}
				FileInputStream in = null;
				try {
					in = new FileInputStream(destFile);
					this.content = parser.parse(in);
					this.setType(DocumentDao.BY_PATH);
				} finally {
					IOUtils.closeQuietly(in);
				}
			} else {
				doc.setType(DocumentDao.BY_CONTENT);
			}
			doc.setContent(this.content);
			doc.setIsbn(this.isbn);
			if (null != destFile)
				doc.setPath(destFile.getCanonicalPath());
			doc.setPublish(this.publish);
			doc.setPublish_time(new Date(System.currentTimeMillis()));
			service.insert(doc);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	public void test() {
		new ViewTools().test();
	}
}
